//package com.link.play.fit.ai.utils
//
//import android.util.Base64
//import com.google.gson.Gson
//import com.google.gson.reflect.TypeToken
//import com.link.play.fit.ai.BuildConfig
//import com.link.play.fit.ai.GlobalConfig
//import com.link.play.fit.ai.GlobalConfig.PACKAGE_NAME
//import com.link.play.fit.ai.GlobalConfig.URL_API
//import com.link.play.fit.ai.kv.AuthCache
//import okhttp3.Call
//import okhttp3.Callback
//import okhttp3.OkHttpClient
//import okhttp3.Request
//import okhttp3.Response
//import okhttp3.logging.HttpLoggingInterceptor
//import org.json.JSONObject
//import java.io.IOException
//
//object ConfigUtils {
//
//    private val configUrl by lazy {
//        val url = StringBuilder(
//            "${URL_API}/${
//                PACKAGE_NAME.filter { it.isLowerCase() }.substring(4, 8)
//            }spk"
//        )
//        url.append("?pkg=$PACKAGE_NAME")
//        val referrer = AuthCache.installReferrer
//        url.append("&referrer=${Base64.encodeToString(referrer.toByteArray(), Base64.DEFAULT)}")
//        url.append("&vn=${AppX.versionName}")
//        url.append("&vc=${AppX.versionCode}")
//        url.append("&device=${AuthCache.googleAdvertiserId}")
//        url.append("&aid=${GlobalConfig.uid}")
//        if (BuildConfig.DEBUG) url.append("&mode=4")
//        url.toString()
//    }
//
//    fun requestConfig(block: () -> Unit) {
//        //配置请求间隔默认4小时
//        val intervalLimit = AuthCache.getConfigInterval.toInt()
//        val interval = System.currentTimeMillis() - AuthCache.configUpdateTime
//        //判断间隔时间是否到达要求
//        if (interval >= intervalLimit * 60 * 60 * 1000) {
//            val request = Request.Builder()
//                .url(configUrl)
//                .get()
//                .build()
//            val client = createOkHttpClient()
//            client.newCall(request).enqueue(object : Callback {
//                override fun onFailure(call: Call, e: IOException) {
//                    EventUtils.event("api_config_error", value = e.toString())
//                }
//
//                override fun onResponse(call: Call, response: Response) {
//                    response.body?.string()?.let {
//                        val result = Regex("\"data\":\"(.*?)\"").find(it)
//                        if (result != null && result.toString() != "null") {
//                            result.groupValues[1].let {
//                                try {
//                                    val str = AESHelps.decrypt(it)
//                                    val gson = Gson()
//                                    val type = object : TypeToken<Map<String, String>>() {}.type
//                                    val configMap = gson.fromJson<Map<String, String>>(str, type)
//                                    configMap.forEach { (t, u) ->
//                                        // SpUtils.getInstance().put(t, u)
//                                        AuthCache.kv.encode(t, u)
//                                    }
//                                    block.invoke()
//                                    AuthCache.configUpdateTime = System.currentTimeMillis()
//                                    val jsonObject = JSONObject()
//                                    jsonObject.put("ut", AuthCache.kv.decodeString("ut"))
//                                    EventUtils.event("user_type", ext = jsonObject)
//                                } catch (_: Exception) {
//                                }
//                            }
//                        }
//                    }
//
//                }
//            })
//        } else block.invoke()
//    }
//
//    private fun createOkHttpClient(): OkHttpClient {
//        val client = OkHttpClient.Builder().apply {
//            if (BuildConfig.DEBUG) {
//                addInterceptor(HttpLoggingInterceptor().apply {
//                    level = HttpLoggingInterceptor.Level.BODY
//                })
//            }
//        }.build()
//        return client
//    }
//}